{% set pypi_name = 'mistral' %}
{% set source = fetch_source('https://tarballs.openstack.org/mistral/mistral-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        Workflow Service for OpenStack cloud
License:        {{ license('Apache-2.0') }}
Group:          Development/Libraries
URL:            https://docs.openstack.org/mistral
Source0:        {{ source | basename}}
Source1:        openstack-mistral-api.service
Source2:        openstack-mistral-engine.service
Source3:        openstack-mistral-executor.service
Source7:        openstack-mistral.tmpfiles
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('PyJWT') }}
BuildRequires:  {{ py3('croniter') }}
BuildRequires:  {{ py3('eventlet') }}
BuildRequires:  {{ py3('fixtures') }}
BuildRequires:  {{ py3('python-ironic-inspector-client') }}
BuildRequires:  {{ py3('jsonschema') }}
BuildRequires:  {{ py3('kazoo') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('mistral-lib') }}
BuildRequires:  {{ py3('networkx') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.messaging') }}
BuildRequires:  {{ py3('oslo.middleware') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('osprofiler') }}
BuildRequires:  {{ py3('paramiko') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('requests-mock') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('stevedore') }}
BuildRequires:  {{ py3('tenacity') }}
BuildRequires:  {{ py3('tooz') }}
BuildRequires:  {{ py3('yaql') }}
BuildRequires:  {{ py3('zake') }}
Requires:       python-mistral = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
%else
BuildRequires:  systemd
%endif

%description
System package - %{name}
Python package - python-{{ pypi_name }}

%package        doc
Summary:        Documentation for OpenStack Workflow Service
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('WSME') }}
BuildRequires:  {{ py3('openstackdocstheme') }}
BuildRequires:  {{ py3('pecan') }}
BuildRequires:  {{ py3('sphinxcontrib-httpdomain') }}
BuildRequires:  {{ py3('sphinxcontrib-pecanwsme') }}

%description    doc
OpenStack Mistral documentaion.
.
This package contains the documentation

%package -n     python-{{ pypi_name }}
Summary:        Mistral Python libraries
Requires:       {{ py3('Babel') }}
Requires:       {{ py3('PyJWT') }}
Requires:       {{ py3('PyYAML') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('WSME') }}
Requires:       {{ py3('alembic') }}
Requires:       {{ py3('cachetools') }}
Requires:       {{ py3('croniter') }}
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('python-ironic-inspector-client') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('mistral-lib') }}
Requires:       {{ py3('networkx') }}
Requires:       {{ py3('oslo.concurrency') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.messaging') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.policy') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('osprofiler') }}
Requires:       {{ py3('paramiko') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('pecan') }}
Requires:       {{ py3('requests') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('stevedore') }}
Requires:       {{ py3('tooz') }}
Requires:       {{ py3('yaql') }}
Requires:       {{ py3('zake') }}

%description -n python-{{ pypi_name }}
Mistral is a workflow service.
Most business processes consist of multiple distinct interconnected steps that need to be executed in a particular order
in a distributed environment. One can describe such process as a set of tasks and task relations and upload such description
to Mistral so that it takes care of state management, correct execution order, parallelism, synchronization and high availability.
.
This package contains the Python libraries.

%package        api
Summary:        OpenStack Mistral API service
Requires:       %{name} = %{version}

%description    api
OpenStack rest API to the Mistral api.
.
This package contains the ReST API.

%package        engine
Summary:        OpenStack Mistral Engine service
Requires:       %{name} = %{version}

%description    engine
OpenStack Mistral Engine service.
.
This package contains the mistral engine, which is one of core services of mistral.

%package        executor
Summary:        OpenStack Mistral Executor service
Requires:       %{name} = %{version}

%description    executor
OpenStack Mistral Executor service.
.
This package contains the mistral executor, which is one of core services of mistral, and
which the API servers will use.

%prep
%if 0%{?rdo}
%autosetup -p1 -n {{ pypi_name }}-%{upstream_version}
%else
%autosetup -p1 -n {{ pypi_name }}-{{upstream_version}}
%endif

%py_req_cleanup

%build
%{py3_build}
export PYTHONPATH="."
oslo-config-generator --config-file tools/config/config-generator.mistral.conf \
--output-file etc/mistral.conf.sample

PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
# remove the sphinx-build leftovers
rm -rf doc/build/html/.{doctrees,buildinfo}

%install
%{py3_install}

install -d -m 755 %{buildroot}%{_sysconfdir}/mistral/
install -d -m 750 %{buildroot}%{_localstatedir}/log/mistral

# install tmpfiles
install -D -m 644 %{SOURCE7} %{buildroot}/%_tmpfilesdir/%name.conf

#systemd unitfiles
install -p -D -m 644 %SOURCE1 %{buildroot}%{_unitdir}/openstack-mistral-api.service
install -p -D -m 644 %SOURCE2 %{buildroot}%{_unitdir}/openstack-mistral-engine.service
install -p -D -m 644 %SOURCE3 %{buildroot}%{_unitdir}/openstack-mistral-executor.service

install -p -D -m 640 etc/mistral.conf.sample %{buildroot}%{_sysconfdir}/mistral/mistral.conf
install -p -D -m 640 etc/logging.conf.sample %{buildroot}%{_sysconfdir}/mistral/logging.conf
install -p -D -m 640 etc/wf_trace_logging.conf.sample %{buildroot}%{_sysconfdir}/mistral/wf_trace_logging.conf
chmod +x %{buildroot}%{_bindir}/mistral*

%pre
%openstack_pre_user_group_create mistral mistral

%check
# we don't want hacking/pep8 tests
rm -r mistral/tests/unit/hacking/
%{__python3} -m stestr.cli run

%post
%tmpfiles_create %{_tmpfilesdir}/%{name}.conf

%post api
%systemd_post openstack-mistral-api.service

%preun api
%systemd_preun openstack-mistral-api.service

%postun api
%systemd_postun_with_restart openstack-mistral-api.service

%post engine
%systemd_post openstack-mistral-engine.service

%preun engine
%systemd_preun openstack-mistral-engine.service

%postun engine
%systemd_postun_with_restart openstack-mistral-engine.service

%post executor
%systemd_post openstack-mistral-executor.service

%preun executor
%systemd_preun openstack-mistral-executor.service

%postun executor
%systemd_postun_with_restart openstack-mistral-executor.service

%files api
%{_unitdir}/openstack-mistral-api.service

%files
%license LICENSE
%dir %{_sysconfdir}/mistral
%config(noreplace) %attr(-, mistral, mistral) %{_sysconfdir}/mistral/*
%{_bindir}/mistral-*
%_tmpfilesdir/%name.conf
%dir %attr(750, mistral, mistral) %{_localstatedir}/log/mistral

%files doc
%license LICENSE
%doc doc/build/html

%files engine
%{_unitdir}/openstack-mistral-engine.service

%files executor
%{_unitdir}/openstack-mistral-executor.service

%files -n python-{{ pypi_name }}
%{python3_sitelib}/{{ pypi_name }}*

%changelog
